package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.admin.mapper.SpChargeStandardsMapper;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.SpChargeStandardsAddDto;
import com.one.group.model.dto.SpChargeStandardsQueryDto;
import com.one.group.model.dto.SpChargeStandardsUpdateDto;
import com.one.group.model.entity.SpChargeStandards;
import com.one.group.model.vo.SpChargeStandardsVo;
import com.one.group.result.Result;
import com.one.group.service.SpChargeStandardsService;
import com.one.group.utils.ResultUtil;
import com.one.group.utils.ThrowUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/SpChargeStandards")
@Tag(name = "收费标准接口")
public class SpChargeStandardsController {

    @Autowired
    private SpChargeStandardsService spChargeStandardsService;
    @Autowired
    private SpChargeStandardsMapper spChargeStandardsMapper;

    @GetMapping("/findByChargeItemId")
    @Operation(summary = "根据收费项目id查询对应的收费标准")
    public Result<List<SpChargeStandards>> find(@RequestParam Long id){
        //1.参数校验
        ThrowUtil.throwIf(id == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtil.success(spChargeStandardsService.findByChargeItemId(id));
    }

    @GetMapping("/findById")
    @Operation(summary = "根据id查询对应的收费标准")
    public Result<SpChargeStandards> findByID(@RequestParam Long id){
        //1.参数校验
        ThrowUtil.throwIf(id == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtil.success(spChargeStandardsMapper.selectOneById(id));
    }
    @PostMapping("/listByQuery")
    @Operation(summary = "查询收费标准")
    public Result listByQuery(@RequestBody SpChargeStandardsQueryDto spChargeStandardsQueryDto){
        //获取租户id
        Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
        PageHelper.startPage(spChargeStandardsQueryDto.getPage(),spChargeStandardsQueryDto.getLimit());
        List<SpChargeStandardsVo> spChargeStandardsVo = spChargeStandardsService.listByQuery(spChargeStandardsQueryDto.getName(),tenant_id);
        PageInfo<SpChargeStandardsVo> pageInfo = new PageInfo<>(spChargeStandardsVo);
        return ResultUtil.success(pageInfo);
    }
    @PostMapping("/add")
    @Operation(summary = "添加收费标准")
    public Result add(@RequestBody SpChargeStandardsAddDto spChargeStandardsAddDto){
        if(spChargeStandardsService.myAdd(spChargeStandardsAddDto)){
            return ResultUtil.success();
        }else{
            return ResultUtil.error(ErrorCode.ADD_ERROR);
        }
    }
    @PutMapping("/update")
    @Operation(summary = "修改收费标准")
    public Result update(@RequestBody SpChargeStandardsUpdateDto spChargeStandardsUpdateDto){
        if(spChargeStandardsService.myUpdateById(spChargeStandardsUpdateDto)){
            return ResultUtil.success();
        }else{
            return ResultUtil.error(ErrorCode.UPDATE_ERROR);
        }
    }
    @PostMapping("/queryListBychargeTypeName")
    @Operation(summary = "根据收费项目名称查询收费标准")
    public Result queryListBychargeTypeName(@RequestBody SpChargeStandardsQueryDto spChargeStandardsQueryDto){
        return ResultUtil.success(spChargeStandardsService.queryListBychargeTypeName(spChargeStandardsQueryDto.getName()));
    }
}
